#include "Log.h"
bool g_bEnable = false;
bool g_bLog    = false;
///////////////////////////////////////////////////////////////
void debug_output(const char *fmt , ...)
{
	if(!g_bEnable)
	{
		va_list ap;
		char buff[65535];
		va_start(ap , fmt);
		if(g_bLog)
		{
			char fileName[32] = {0};
			time_t now = time(0);
			 tm *p = localtime(&now);
    		sprintf(fileName, "log/%.4d%.2d%.2d.log", p->tm_year + 1900,
        	p->tm_mon + 1, p->tm_mday);
   			 // 以追加写方式打开信息文件
   			 FILE *file = fopen(fileName, "a");
   			 if (0 != file)
   			 {
				sprintf(buff, "%d:%d:%d : ",p->tm_hour, p->tm_min, p->tm_sec);
				fwrite(buff, 1, strlen(buff), file); 
				vsnprintf(buff, sizeof(buff), fmt, ap);
				fwrite(buff, 1, strlen(buff), file);
  		      	// 写入回车换行
//       			 char nextline[] = {0x0D};
  //     			 fwrite(nextline, 1, sizeof(nextline), file);

       			 // 关闭文件
		        fclose(file);
   			 }	
		}
		else
		{		
			current(buff);
			std::cout<<buff<<" : ";
			vsnprintf(buff , sizeof(buff) ,  fmt , ap);
			std::cout<<buff;
		}
		va_end(ap);
	}
}

void current(char* t)
{
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep); /*取得当地时间*/
				
	sprintf(t,"%d-%d-%d %d:%d:%d",
			1900+p->tm_year,
			1+p->tm_mon,
			p->tm_mday,
			p->tm_hour, 
			p->tm_min, 
			p->tm_sec);
}
void setLogEnable(bool bEnable, bool bFile)
{
	g_bEnable = bEnable;
	g_bLog    = bFile;
}
